<?

function getIP()
{
	$ip;

	if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
	else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
	else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
	else $ip = false;

	return $ip;
}

function flash($flash_message)
{
	$CI =& get_instance();
	$CI->session->set_flashdata('flash_message', $flash_message);
}

function _get_sunday_of_the_week($date)
{
		$ts = strtotime($date);
		$start = (date('w', $ts) == 0) ? $ts : strtotime('last sunday', $ts);

		return (object)(array('sunday'=>date('Y-m-d', $start),
			'saturday'=>date('Y-m-d', strtotime('next saturday', $start))));
}

function send_email($to, $subject, $message, $from_email = 'noreply@ACycleHouse.com', $from_name = 'A Cycle House Admin')
{
	$CI =& get_instance();

//	$CI->load->library('email');
	$config['protocol']    = 'smtp';
    $config['smtp_host']    = 'ssl://smtp.gmail.com';
    $config['smtp_port']    = '465';
    $config['smtp_timeout'] = '7';
    $config['smtp_user']    = 'acyclehouse@gmail.com';
    $config['smtp_pass']    = 'asda1231';
    $config['charset']    = 'utf-8';
    $config['newline']    = "\r\n";
	$config['mailtype'] = 'html';

	$CI->email->initialize($config);

	$CI->email->from($from_email, $from_name);
	$CI->email->to($to);

	$CI->email->subject($subject);
	$CI->email->message($message);

	$CI->email->send();
}

function generate_db()
{
	$CI =& get_instance();

	$CI->load->dbutil();
	$prefs = array(
		'tables' => array('activities', 'ci_sessions', 'items', 'roles','sales','sales_items','users'),  // Array of tables to backup.
		'format' => 'txt',
		'add_drop' => TRUE,
		'add_insert' => TRUE,
		'newline' => "\n"
	);
	return $CI->dbutil->backup($prefs);
}